package com.example.yls.demoa;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button btn_point;
    private MyButton myButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_point = findViewById(R.id.btn_point);
        myButton = findViewById(R.id.btn_color2);

        //平移
        btn_point.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Point start = new Point(100, 100);
                Point end = new Point(400, 600);

                ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator()
                        , start, end);
                valueAnimator.setDuration(3000);

                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        Point point = (Point) valueAnimator.getAnimatedValue();
                        Log.e(TAG, "x =  " + point.getX() + " " + "y = " + point.getY());
                        btn_point.setTranslationX(point.getX());
                        btn_point.setTranslationY(point.getY());
                        btn_point.requestLayout();
                    }
                });
                valueAnimator.start();
            }
        });

        //平移
        findViewById(R.id.btn_t).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn_point,
                        "translationX", btn_point.getTranslationX(), 500);

                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float y = (float) objectAnimator.getAnimatedValue();
                        btn_point.setTranslationY(y);
                    }
                });
                objectAnimator.start();

            }
        });

        //缩放
        findViewById(R.id.btn_s).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn_point,
                        "scaleX", 1, 3);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float y = (float) objectAnimator.getAnimatedValue();
                        btn_point.setScaleY(y);
                    }
                });
                objectAnimator.start();
            }
        });

        //旋转
        findViewById(R.id.btn_r).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn_point,
                        "rotation", 0, 360);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();
            }
        });

        //透明度
        findViewById(R.id.btn_p).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn_point,
                        "alpha", 1f, 0f, 1f);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();
            }
        });

        //组合
        findViewById(R.id.btn_set).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //代码实现
//                AnimatorSet animatorSet = new AnimatorSet();
//                final ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(btn_point,
//                        "translationX", btn_point.getTranslationX(), 500);
//                final ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(btn_point,
//                        "scaleX", 1, 3);
//                final ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(btn_point,
//                        "rotation", 0, 360);
//                final ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(btn_point,
//                        "alpha", 1f, 0f, 1f);
//
//                animatorSet.play(objectAnimator3).with(objectAnimator2)
//                        .before(objectAnimator4).after(objectAnimator1);
//                animatorSet.setDuration(3000);
//                animatorSet.start();

                //xml实现
                AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.this,
                        R.animator.set_animation);
                animatorSet.setTarget(btn_point);
                animatorSet.start();


            }
        });

        findViewById(R.id.btn_color).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyColor start = new MyColor();
                start.red = 255;

                MyColor end = new MyColor();
                end.green = 255;

                ObjectAnimator objectAnimator = ObjectAnimator.ofObject(myButton, "color",
                        new MyColorEvaluator(), start, end);
                objectAnimator.setDuration(5000);
                objectAnimator.start();
            }
        });
    }
}
